package com.game.sdk;

import com.game.sdk.domain.LoginErrorMsg;
import com.game.sdk.domain.LogincallBack;
import com.game.sdk.domain.OnLoginListener;
import com.game.sdk.domain.OnPaymentListener;
import com.game.sdk.domain.PaymentCallbackInfo;
import com.game.sdk.domain.PaymentErrorMsg;
import com.game.sdk.util.Constants;
import com.game.sdk.util.Logger;
import com.game.sdk.util.MResource;
import com.game.sdk.util.Md5Util;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 整个activity是一个demo界面
 * */
public class YTSDKActivity extends Activity implements OnClickListener {

	public YTSDKManager ttwsdkmanager;
	private EditText et_money;
	private TextView tv_msg;
	private Button btn_login, btn_charger;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ttwsdkmanager = YTSDKManager.getInstance(this);

		setContentView(MResource.getIdByName(getApplication(),
				Constants.Resouce.LAYOUT, "ttw_sdk"));
		btn_login = (Button) findViewById(MResource.getIdByName(
				getApplication(), "id", "btn_login"));
		btn_charger = (Button) findViewById(MResource.getIdByName(
				getApplication(), "id", "btn_charger"));
		tv_msg = (TextView) findViewById(MResource.getIdByName(
				getApplication(), "id", "tv_msg"));

		et_money = (EditText) findViewById(MResource.getIdByName(
				getApplication(), "id", "et_money"));

		btn_login.setOnClickListener(this);
		btn_charger.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		// 登录事件
		if (null != btn_login && btn_login.getId() == v.getId()) {
			ttwsdkmanager.showLogin(this, true, new OnLoginListener() {
				@Override
				public void loginSuccess(LogincallBack logincallback) {
					ttwsdkmanager.showFloatView();// 展示浮点
					Toast.makeText(
							// 需要游戏接入人员去将回调logincallback的值获取到
							// mem_id和user_token做后续工作
							YTSDKActivity.this,
							"用户id：" + logincallback.mem_id + " " + "用户token："
									+ logincallback.user_token,
							Toast.LENGTH_LONG).show();
				}

				@Override
				public void loginError(LoginErrorMsg errorMsg) {
					// 登录失败的借口回调，如果游戏要用到这个值需要去errorMsg， errorMsg 的
					// errorMsg.code;// 登录失败的状态码
					// errorMsg.msg;// 登录失败的消息提示
				}
			});
			return;
		}
		// 充值事件
		if (null != btn_charger && btn_charger.getId() == v.getId()) {
			String money_str = et_money.getText().toString().trim();
			String money = "1";
			if (!TextUtils.isEmpty(money_str) && !"".equals(money_str)) {
				money = money_str;
			}
			ttwsdkmanager.showPay(this, "roleid", money, "1", "魔神", "金币", "",
					"描述", new OnPaymentListener() {
						@Override
						public void paymentSuccess(
								PaymentCallbackInfo callbackInfo) {
							// TODO Auto-generated method stub
							Toast.makeText(
									getApplication(),
									"充值金额数：" + callbackInfo.money + " 消息提示："
											+ callbackInfo.msg,
									Toast.LENGTH_LONG).show();
						}

						@Override
						public void paymentError(PaymentErrorMsg errorMsg) {
							// TODO Auto-generated method stub
							Toast.makeText(
									getApplication(),
									"充值失败：code:" + errorMsg.code
											+ "  ErrorMsg:" + errorMsg.msg
											+ "  预充值的金额：" + errorMsg.money,
									Toast.LENGTH_LONG).show();
						}
					});

			return;
		}

	}

	@Override
	protected void onDestroy() {// 在退出的时候就必须要调用ttwsdkmanager.recycle()
								// recycle()销毁sdk的资源  
		// ttwsdkmanager.recycle();//游戏退出必须调用
		super.onDestroy();
	};

	@Override
	protected void onStop() {//从窗体移除浮点
		ttwsdkmanager.removeFloatView();
		super.onStop();
	}

	@Override
	protected void onResume() {//显示浮点
		ttwsdkmanager.showFloatView();
		super.onResume();
	}

}
